/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package feuerwehr;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author GundlaPe
 */
public class Kartendienst {

    /**
     * Kartendienst4 (Geocoding: Google/Karte: OSM)
     * @param jIconLabel Ein JIconLabel welches die Karte bekommt (quasi geschenkt!)
     * @param sLat Latitude 
     * @param sLon Longitude
     */
    public static void kartenDienst(JLabel jIconLabel, String sLat, String sLon) {
        try {
            String strURL = "http://staticmap.openstreetmap.de/staticmap.php?center=" + sLat + "," + sLon + "&zoom=17&size=900x1200&markers=" + sLat + "," + sLon + ",red-pushpin";
            imageIcon = new ImageIcon(new URL(strURL));
            jIconLabel.setIcon(imageIcon);
            // System.err.println("DEBUG>> " + sLat + " / " + sLon);
            // System.err.println("DEBUG>> " + strURL);
        } catch (MalformedURLException exception) {
            System.err.println("Error: " + exception);
        }
    }

    public static void kartenDienstHTML(JEditorPane jEditorPane, String sLat, String sLon) {
        try {
            String strURL = "http://staticmap.openstreetmap.de/staticmap.php?center=" + sLat + "," + sLon + "&zoom=17&size=900x1200&markers=" + sLat + "," + sLon + ",red-pushpin";
            System.err.println("DEBUG>> " + strURL);
            
            jEditorPane.setPage(strURL); 
        } catch (MalformedURLException exception) {
            System.err.println("Error1: " + exception);
        } catch (IOException exception) {
            System.err.println("Error2: " + exception);
        }
    }

    /**
     * Kartendienst4 (Geocoding: Google/Karte: OSM)
     * @param jIconLabel Ein JIconLabel welches die Karte bekommt (quasi geschenkt!)
     * @param sLat Latitude 
     * @param sLon Longitude
     * @param sLatLon
     */
    public static void kartenDienstAlle(JLabel jIconLabel, String sLat, String sLon, String sLatLon, String zoom) {
        try {
            String strURL = "http://staticmap.openstreetmap.de/staticmap.php?center=" + sLat + "," + sLon + "&zoom=" + zoom + "&size=1200x1200&" + sLatLon;
            // System.err.println("DEBUG>> sLatLon: " + sLatLon);
            // System.err.println("DEBUG>> URL: " + strURL);
            imageIcon = new ImageIcon(new URL(strURL));
            jIconLabel.setIcon(imageIcon);
        } catch (MalformedURLException exception) {
            System.err.println("Error: " + exception);
        }
    }
    
    /**
     * Latitude und Longitude für einen gegebenen Ort berechnen
     * @param sStrasse Die Strasse
     * @param sPLZ Die PLZ
     * @param sOrt Der Ort
     * @return Array mit Lat/lon
     */
    public static String[] getLatLon(String sStrasse, String sPLZ, String sOrt) {
        String [] s = {"", ""};
        String sLat="", sLon="";
        try {
            sStrasse = sStrasse.replace("Ä", "%C3%84");
            sStrasse = sStrasse.replace("Ö", "%C3%96");
            sStrasse = sStrasse.replace("Ü", "%C3%9C");
            sStrasse = sStrasse.replace("ä", "%C3%A4");
            sStrasse = sStrasse.replace("ö", "%C3%B6");
            sStrasse = sStrasse.replace("ü", "%C3%BC");
            sStrasse = sStrasse.replace("ß", "%C3%9F");
            sStrasse = sStrasse.replace(" ", "+");

            String result = "";
            URL url = new URL("http://maps.googleapis.com/maps/api/geocode/xml?address=" + sStrasse + "," + sPLZ + "+" + sOrt + "&sensor=false");
            URLConnection connection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;

            boolean bFlag = false;
            while ((line = in.readLine()) != null) {
                if (line.indexOf("<location>") != -1) {
                    bFlag = true;
                }
                if (bFlag == true) {
                    if (line.indexOf("<lat>") != -1) {
                        sLat = line.substring(line.indexOf("<lat>"), line.length());
                        sLat = sLat.substring(0, sLat.indexOf("</lat>"));
                        sLat = sLat.substring(5, sLat.length() - 1);
                    }
                    if (line.indexOf("<lng>") != -1) {
                        sLon = line.substring(line.indexOf("<lng>"), line.length());
                        sLon = sLon.substring(0, sLon.indexOf("</lng>"));
                        sLon = sLon.substring(5, sLon.length() - 1);
                        break;
                    }
                }
            }
        } catch (IOException exception) {
            System.err.println("Error: " + exception);
        }
        s[0]=sLon; s[1]=sLat;
        return s;
    }
    
    private static ImageIcon imageIcon = new ImageIcon();
}
